const got = require('got')
const cheerio = require('cheerio')
const Category = require('../models').Category
const url = require('url')

const rootUrl = 'http://www.howzhi.com/'

// let count = 0
// getTopCategories(url.resolve(rootUrl, 'courses'), '#content-container .tab-head li', 0)
// .then(result => {
//   return Promise.all(result.map(item => new Category(item).save()))
// })
// .then((data) => {
// 1. 遍历顶级分类数据集
// 2. 通过顶级分类中的 link 找对应的子集分类
// 3. 找到所有的子级分类，保存到数据库，使用当前顶级分类的 父 ID
// 4. 所有操作执行结束，打印结果
// data.forEach(topCategory => {
//   getTopCategories(url.resolve(rootUrl, topCategory.link), '#content-container .tab-body li', topCategory._id)
//     .then(result => {
//       result.forEach(item => {
//         new Category(item).save()
//       })
//     })
// })
// console.log(data)

//   data.forEach(function (topCategory) {
//     topCategory._id = topCategory._id.toString()
//   })
//   return Promise.all(data.map(topCategory => {
//     return getTopCategories(url.resolve(rootUrl, topCategory.link), '#content-container .tab-body li', topCategory._id)
//   }))
// })
// .then(data => {
//   return Promise.all(reduceDimension(data).map(item => new Category(item).save()))
// })
// .then(data => {
//   console.log('保存成功了')
// })
// .catch(err => {
//   console.log(err)
// })


function getTopCategories(url, selector, pid) {
  return new Promise((resolve, reject) => {
    got(url)
      .then(res => {
        const $ = cheerio.load(res.body)
          // '#content-container .tab-head li'
        const topCategories = $(selector).slice(1)
        const result = []
        topCategories.each((index, topCategory) => {
          const a = $(topCategory).find('a')
          result.push({
            name: a.text().trim(),
            pid: pid,
            link: a.attr('href').trim()
          })
        })
        resolve(result)
      })
      .catch(err => {
        reject(err)
      })
  })
}

function reduceDimension(arr) {
  return Array.prototype.concat.apply([], arr)
}

getAllCourses('http://www.howzhi.com/channel/IT-internet')
  .then(data => {
    console.log(data)
  })
  .catch(err => {
    console.log(err)
  })

// 'http://www.howzhi.com/channel/IT-internet'
function getAllCourses(pageUrl) {
  return new Promise((resolve, reject) => {
    const result = [];
    (function getCurrentPageCourses(pageUrl) {
      got(pageUrl)
        .then(res => {
          const $ = cheerio.load(res.body)
          const coursesContainer = $('.course.list.row')
          const courses = coursesContainer.find('> div')
          courses.each((index, course) => {
            const $courseInfo = $(course).find('.course-info')
            const item = $courseInfo.find('.title a').text().trim()
            result.push(item)
            console.log(item)
          })
          const nextI = coursesContainer.find('.es-icon.es-icon-chevronright')
          if (nextI.length > 0) {
            const nextUrl = nextI.parent().attr('href')
            getCurrentPageCourses(url.resolve(rootUrl, nextUrl))
          } else {
            resolve(result)
          }
        })
        .catch(err => {
          reject(err)
        })
    })(pageUrl)
  })
}
